package com.campus.backend.service;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.campus.backend.bean.canteen.CanteenQueryParam;
import com.campus.backend.bean.canteen.CreateCanteenBean;
import com.campus.backend.bean.canteen.UpdateCanteenBean;
import com.campus.backend.entity.Canteen;
import com.campus.backend.mapper.CanteenMapper;
import com.campus.backend.vo.canteen.CanteenVo;
import com.campus.common.bean.common.PageUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 食堂表 服务实现类
 * </p>
 *
 * @author ZhaoYuJie
 * @since 2025-05-21
 */
@Service
public class CanteenService extends ServiceImpl<CanteenMapper, Canteen> {

    /**
     * 分页查询食堂列表
     *
     * @param param 查询参数
     * @return 分页结果
     */
    public PageInfo<CanteenVo> getCanteenListPage(CanteenQueryParam param) {
        PageHelper.startPage(param.getPageNo(), param.getPageSize());
        PageInfo<Canteen> pageInfo = new PageInfo<>(getCanteenList(param));

        // 转换为VO对象
        List<CanteenVo> voList = pageInfo.getList().stream().map(canteen -> {
            CanteenVo vo = new CanteenVo();
            BeanUtil.copyProperties(canteen, vo);
            
            // 设置状态名称
            vo.setStatusName(canteen.getStatus() == 1 ? "启用" : "禁用");
            
            return vo;
        }).collect(java.util.stream.Collectors.toList());

        return PageUtil.convertPageInfo(pageInfo, voList);
    }

    /**
     * 查询食堂列表
     *
     * @param param 查询参数
     * @return 食堂列表
     */
    private List<Canteen> getCanteenList(CanteenQueryParam param) {
        LambdaQueryWrapper<Canteen> queryWrapper = new LambdaQueryWrapper<>();
        
        if (StringUtils.hasText(param.getName())) {
            queryWrapper.like(Canteen::getName, param.getName());
        }
        
        if (StringUtils.hasText(param.getFloor())) {
            queryWrapper.like(Canteen::getFloor, param.getFloor());
        }
        
        if (param.getStatus() != null) {
            queryWrapper.eq(Canteen::getStatus, param.getStatus());
        }
        
        queryWrapper.orderByDesc(Canteen::getCreateTime);
        
        return this.list(queryWrapper);
    }

    /**
     * 根据ID获取食堂详情
     *
     * @param canteenId 食堂ID
     * @return 食堂VO
     */
    public CanteenVo getCanteenById(Integer canteenId) {
        Canteen canteen = this.getById(canteenId);
        if (canteen == null) {
            throw new RuntimeException("食堂不存在");
        }
        
        CanteenVo vo = new CanteenVo();
        BeanUtil.copyProperties(canteen, vo);
        
        // 设置状态名称
        vo.setStatusName(canteen.getStatus() == 1 ? "启用" : "禁用");
        
        return vo;
    }

    /**
     * 创建食堂
     *
     * @param createCanteenBean 食堂信息
     * @return 食堂ID
     */
    public Integer createCanteen(CreateCanteenBean createCanteenBean) {
        Canteen canteen = new Canteen();
        BeanUtil.copyProperties(createCanteenBean, canteen);
        
        // 设置默认值
        if (canteen.getStatus() == null) {
            canteen.setStatus(1);
        }
        
        canteen.setCreateTime(LocalDateTime.now());
        canteen.setUpdateTime(LocalDateTime.now());
        
        this.save(canteen);
        return canteen.getId();
    }

    /**
     * 更新食堂
     *
     * @param updateCanteenBean 食堂信息
     */
    public void updateCanteen(UpdateCanteenBean updateCanteenBean) {
        Canteen existCanteen = this.getById(updateCanteenBean.getId());
        if (existCanteen == null) {
            throw new RuntimeException("食堂不存在");
        }
        
        Canteen canteen = new Canteen();
        BeanUtil.copyProperties(updateCanteenBean, canteen);
        canteen.setUpdateTime(LocalDateTime.now());
        
        this.updateById(canteen);
    }

    /**
     * 删除食堂
     *
     * @param canteenId 食堂ID
     */
    public void deleteCanteen(Integer canteenId) {
        Canteen canteen = this.getById(canteenId);
        if (canteen == null) {
            throw new RuntimeException("食堂不存在");
        }
        
        this.removeById(canteenId);
    }

    /**
     * 批量删除食堂
     *
     * @param canteenIds 食堂ID列表
     */
    public void batchDeleteCanteen(List<Integer> canteenIds) {
        if (canteenIds == null || canteenIds.isEmpty()) {
            throw new RuntimeException("请选择要删除的食堂");
        }
        
        this.removeByIds(canteenIds);
    }

    /**
     * 更新食堂状态
     *
     * @param canteenId 食堂ID
     * @param status 状态
     */
    public void updateCanteenStatus(Integer canteenId, Integer status) {
        Canteen canteen = this.getById(canteenId);
        if (canteen == null) {
            throw new RuntimeException("食堂不存在");
        }
        
        LambdaUpdateWrapper<Canteen> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Canteen::getId, canteenId)
                .set(Canteen::getStatus, status)
                .set(Canteen::getUpdateTime, LocalDateTime.now());
        
        this.update(updateWrapper);
    }
}
